package com.xxxx.oa.web.action;

import com.xxxx.oa.bean.Dept;
import com.xxxx.oa.utils.DBUtil;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet({"/dept/list","/dept/detail","/dept/delete","/dept/save","/dept/modify"})
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        String servletPath = request.getServletPath();
        if(session!=null&&session.getAttribute("loginName")!=null){
            if("/dept/list".equals(servletPath)){
                doList(request,response);
            }else if("/dept/detail".equals(servletPath)){
                doDetail(request,response);
            }else if("/dept/delete".equals(servletPath)){
                doDel(request,response);
            }else if("/dept/save".equals(servletPath)){
                doSave(request,response);
            }else if("/dept/modify".equals(servletPath)){
                doModify(request,response);
            }
        }else{
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }

    }

    private void doModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String dno = request.getParameter("dno");
        String dname = request.getParameter("dname");
        String dloc = request.getParameter("dloc");
        Connection conn=null;
        PreparedStatement ps=null;
        int count=0;
        try {
            conn = DBUtil.getConnection();
            String sql="update dept set dname=?,dloc=? where dno=?";
            ps=conn.prepareStatement(sql);

            ps.setString(1,dname);
            ps.setString(2,dloc);
            ps.setString(3,dno);
            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,null);
        }
        if(count==1){
            response.sendRedirect(request.getContextPath()+"/dept/list");
        }


    }

    private void doSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String dno = request.getParameter("dno");
        String dname = request.getParameter("dname");
        String dloc = request.getParameter("dloc");
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        int count=0;
        try {
            conn = DBUtil.getConnection();
            String sql="insert into dept(dno,dname,dloc)values(?,?,?)";
            ps=conn.prepareStatement(sql);
            ps.setString(1,dno);
            ps.setString(2,dname);
            ps.setString(3,dloc);

            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        if(count==1){
            response.sendRedirect(request.getContextPath()+"/dept/list");
        }

    }

    private void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String dno = request.getParameter("dno");
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        int count=0;
        try {
            conn = DBUtil.getConnection();
            String sql="delete from dept where dno=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,dno);
            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        if(count==1){
            response.sendRedirect(request.getContextPath()+"/dept/list");
        }
    }

    private void doDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Dept dept=new Dept();
        String dno = request.getParameter("dno");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select dno,dname,dloc from dept where dno=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, dno);
            rs = ps.executeQuery();
            if (rs.next()) {
                String dname = rs.getString("dname");
                String dloc = rs.getString("dloc");
                dept.setDno(dno);
                dept.setDname(dname);
                dept.setDloc(dloc);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        request.setAttribute("dept",dept);

//        request.getRequestDispatcher("/detail.jsp").forward(request,response);

        String forward="/"+request.getParameter("f")+".jsp";
        request.getRequestDispatcher(forward).forward(request,response);


    }

    private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ArrayList<Dept> depts = new ArrayList<>();
        Connection conn=null;

        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            conn= DBUtil.getConnection();
            String sql="select dno,dname,dloc from dept";
            ps= conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()){
                String dno=rs.getString("dno");
                String dname=rs.getString("dname");
                String dloc = rs.getString("dloc");
                Dept dept=new Dept();
                dept.setDno(dno);
                dept.setDname(dname);
                dept.setDloc(dloc);

                depts.add(dept);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(conn,ps,rs);
        }

        request.setAttribute("deptList",depts);
        request.getRequestDispatcher("/list.jsp").forward(request,response);


    }
}
